package com.darwin.darwinUI.containers
{
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.uiconst.UIAlign;
	
	import flash.events.Event;
	import flash.sampler.getInvocationCount;

	/**
	 * 包括水平及垂直布局
	 * @author vanCopper
	 */
	public class Box extends ContainerBase
	{
		private var _align:String = UIAlign.LAYOUT_HORIZONTAL;
		private var _itemDistance:int = 0;
		private var _layoutFunc:Function;
		
		
		public function Box()
		{
			super();
		}
		
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			updateLayout();
			refresh();
		}
		
		override public function addItem(item:UIComponent):void
		{
			super.addItem(item);
		}
		
		override public function addItemAt(item:UIComponent, index:int):void
		{
			super.addItemAt(item,index);
		}
		
		override public function removeItem(item:UIComponent):void
		{
			super.removeItem(item);
		}
		
		override public function removeItemAt(index:int):UIComponent
		{
			var item:UIComponent = super.removeItemAt(index);
			return item;
		}
		
		/**
		 * 更新布局 
		 * 
		 */		
		override protected function updateLayout():void
		{
			super.updateLayout();
			if(layoutFunc != null)
			{
				layoutFunc(itemList,align,itemDistance);
			}else
			{
				var childNum:int = 0;
				var item:UIComponent;
				var preItem:UIComponent;
				if(align == UIAlign.LAYOUT_HORIZONTAL)
				{
					while(childNum < _container.numChildren)
					{
						item = _container.getChildAt(childNum) as UIComponent;
						item.y = 0;
						if(childNum == 0)
						{
							item.x = 0;
						}else
						{
							item.x = preItem.x + preItem.width + itemDistance;
						}
						preItem = item;
						childNum ++;
					}
				}else if(align == UIAlign.LAYOUT_VERTICAL)
				{
					while(childNum < _container.numChildren)
					{
						item = _container.getChildAt(childNum) as UIComponent;
						item.x = 0;
						if(childNum == 0)
						{
							item.y = 0;
						}else
						{
							item.y = preItem.y + preItem.height + itemDistance;
						}
						preItem = item;
						childNum ++;
					}
				}
			}
		}
		
		
		/**
		 * 设置容器布局 
		 */
		public function get align():String
		{
			return _align;
		}

		/**
		 * @private
		 */
		public function set align(value:String):void
		{
			_align = value;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 项间距 
		 */
		public function get itemDistance():int
		{
			return _itemDistance;
		}

		/**
		 * @private
		 */
		public function set itemDistance(value:int):void
		{
			_itemDistance = value;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 外部控制更新布局方法，通过设置Function来实现布局更新
		 * layoutFunc(itemList:Vector.<UIComponent>,align,itemDistance); 
		 */
		public function get layoutFunc():Function
		{
			return _layoutFunc;
		}

		/**
		 * @private
		 */
		public function set layoutFunc(value:Function):void
		{
			_layoutFunc = value;
		}

	}
}